package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.ICardDao;
import com.qymerge.manage.base.ro.CardRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: CardDaoImpl</p>
 *<p>Description: 优惠券管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class CardDaoImpl extends BaseDao implements ICardDao {

    @Override
    public List list(CardRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = ParamKey.NameSpace.TableSelect.card;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and id=:id";
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCardname())) {
            sql += " and cardname like :cardname";
            paramMap.put("cardname", "%" + ro.getCardname() + "%");
        }

        if (ro.getNum() > -1) {
            sql += " and num >=:num";
            paramMap.put("num", ro.getNum());
        }

        if (ro.getLnum() > -1) {
            sql += " and lnum >=:lnum";
            paramMap.put("lnum", ro.getLnum());
        }

        // 分页数据获取
        sql += " order by id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<CardRO>(CardRO.class));
    }

    @Override
    public int total(CardRO ro) {
        String sql = "select count(1) from card where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and id=:id";
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCardname())) {
            sql += " and cardname like :cardname";
            paramMap.put("cardname", "%" + ro.getCardname() + "%");
        }

        if (ro.getNum() > -1) {
            sql += " and num >=:num";
            paramMap.put("num", ro.getNum());
        }

        if (ro.getLnum() > -1) {
            sql += " and lnum >=:lnum";
            paramMap.put("lnum", ro.getLnum());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public CardRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.card);
        sql.append("and id=?");
        RowMapper<CardRO> rowMapper = new BeanPropertyRowMapper<CardRO>(CardRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<CardRO> findAll(CardRO ro) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.card);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCardname())) {
            sql.append(" and cardname like :cardname");
            paramMap.put("cardname", "%" + ro.getCardname() + "%");
        }

        if (ro.getNum()>-1) {
            sql.append(" and num >= :num");
            paramMap.put("num", ro.getNum());
        }

        if (ro.getLnum()>-1) {
            sql.append(" and lnum >= :lnum");
            paramMap.put("lnum", ro.getLnum());
        }

        sql.append(" order by id desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<CardRO>(CardRO.class));
    }

    @Override
    public int install(CardRO ro) {
        StringBuilder sql = new StringBuilder("insert into card(cardname, num, lnum) values(?,?,?)");

        List params = new ArrayList();
        params.add(ro.getCardname());
        params.add(ro.getNum());
        params.add(ro.getLnum());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(CardRO ro) {
        StringBuilder sql = new StringBuilder("update card set cardname=?,num=?,lnum=? ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("cardname", ro.getCardname());
        paramsMap.put("num", ro.getNum());
        paramsMap.put("lnum", ro.getLnum());

        sql.append("where id=?");
        paramsMap.put("id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from card where id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }


}
